﻿using System.Collections.Specialized;
using System.Text;

namespace System.Web.Mvc
{
    /// <summary>
    /// HtmlHelper 分页扩展
    /// </summary>
    public static class PagerHelper
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="id">分页id</param>
        /// <param name="pager">分页实体类</param>
        /// <returns>HTML</returns>
        public static MvcHtmlString Pager(this HtmlHelper helper, string id, PagerInfo pager)
        {
            if (pager == null) return MvcHtmlString.Empty;

            TagBuilder builder = new TagBuilder("pagerBuilder");
            builder.IdAttributeDotReplacement = "_";
            builder.GenerateId(id);
            builder.InnerHtml = GetNormalPage(pager);
            return MvcHtmlString.Create(builder.ToString());
        }

        /// <summary>
        /// 获取普通分页
        /// </summary>
        private static string GetNormalPage(PagerInfo pager)
        {
            StringBuilder url = new StringBuilder();
            url.Append(HttpContext.Current.Request.Url.AbsolutePath + "?page={0}");

            if (!string.IsNullOrEmpty(pager.Append_url)) //页面多参数
            {
                url.Append("&" + pager.Append_url);
            }

            NameValueCollection collection = HttpContext.Current.Request.QueryString;
            string[] keys = collection.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i].ToLower() != "page")
                    url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr><td>");
            sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页&nbsp;&nbsp;", pager.RecordCount, pager.PageCount, pager.CurrentIndex);
            if (pager.CurrentIndex == 1)
                sb.Append("<span>首页</span>&nbsp;");
            else
            {
                string url1 = string.Format(url.ToString(), 1);
                sb.AppendFormat("<span><a href={0}>首页</a></span>&nbsp;", url1);
            }
            if (pager.CurrentIndex > 1)
            {
                string url1 = string.Format(url.ToString(), pager.CurrentIndex - 1);
                sb.AppendFormat("<span><a href={0}>上一页</a></span>&nbsp;", url1);
            }
            else
            {
                sb.Append("<span>上一页</span>&nbsp;");
            }

            sb.Append(GetNumericPage(pager, url.ToString()));

            if (pager.CurrentIndex < pager.PageCount)
            {
                string url1 = string.Format(url.ToString(), pager.CurrentIndex + 1);
                sb.AppendFormat("<span><a href={0}>下一页</a></span>&nbsp;", url1);
            }
            else
                sb.Append("<span>下一页</span>&nbsp;");

            if (pager.CurrentIndex == pager.PageCount)
                sb.Append("<span>末页</span>&nbsp;");
            else
            {
                string url1 = string.Format(url.ToString(), pager.PageCount);
                sb.AppendFormat("<span><a href={0}>末页</a></span>&nbsp;", url1);
            }

            return sb.ToString();
        }

        /// <summary>
        /// 获取数字分页
        /// </summary>
        private static string GetNumericPage(PagerInfo pager, string url)
        {
            int k = pager.CurrentIndex / 10;
            int m = pager.CurrentIndex % 10;
            StringBuilder sb = new StringBuilder();
            if (pager.CurrentIndex / 10 == pager.PageCount / 10)
            {
                if (m == 0)
                {
                    k--;
                    m = 10;
                }
                else
                {
                    m = pager.PageCount % 10;
                }
            }
            else
                m = 10;
            for (int i = k * 10 + 1; i <= k * 10 + m; i++)
            {
                if (i == pager.CurrentIndex)
                    sb.AppendFormat("<span><font color=red><b>{0}</b></font></span>&nbsp;", i);
                else
                {
                    string url1 = string.Format(url.ToString(), i);
                    sb.AppendFormat("<span><a href={0}>{1}</a></span>&nbsp;", url1, i);
                }
            }

            return sb.ToString();
        }
    }


    /// <summary>
    /// 分页实体类
    /// </summary>
    public class PagerInfo
    {
        /// <summary>
        /// 当前页Index
        /// </summary>
        public int CurrentIndex { get; set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; set; }

        /// <summary>
        /// 附加url,主要用于多参数,非必须
        /// </summary>
        public string Append_url { get; set; }

        /// <summary>
        /// 总页数，计算得出
        /// </summary>
        public int PageCount
        {
            get
            {
                if (RecordCount > 0 && PageSize > 0)
                {
                    return RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
